package com.kx.smartdevice.controller;

import com.github.pagehelper.PageInfo;
import com.kx.smartdevice.dto.R;
import com.kx.smartdevice.pojo.Device;
import com.kx.smartdevice.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName DevicesController
 * @Author Sun
 * @Date 2021/6/1 14:19
 */
@RestController
@RequestMapping("/devices")
public class DeviceController {

    @Autowired
    private DeviceService deviceService;

    @GetMapping("/devices")
    public R getAllDevices(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit, String address, @RequestParam(defaultValue = "-100") Integer status, @RequestParam(defaultValue = "-100")  int direction, @RequestParam(defaultValue = "-100") int areaid, @RequestParam(defaultValue = "-100") int categoryid) {
        PageInfo<Device> pageInfo = deviceService.getAllDevices(page, limit, address, status, direction,areaid,categoryid);
        return R.setOK(pageInfo);
    }


    @PutMapping("/device")
    public R AddDevice(@RequestBody Device device) {
        deviceService.AddDevice(device);
        return R.setOK(null);
    }

    @PostMapping("/device")
    public R updateDevice(@RequestBody Device device) {
        deviceService.updateDevice(device);
        return R.setOK(null);
    }

    @DeleteMapping("/devices")
    public R deleteDevices(String[] ids) {
        deviceService.deleteDevices(ids);
        return R.setOK(null);
    }
    @GetMapping("/device/{id}")
    public R getDeviceById(@PathVariable Long id) {
        Device device = deviceService.getDeviceById(id);
        return R.setOK(device);
    }

    /**
     *
     * @param uuid  要针对那个设备做事情
     * @param command  我们需要发送的明令
     * @return
     */
    @PostMapping("/command/{uuid}/{command}")
    public R sendControl(@PathVariable String uuid, @PathVariable String command) {
        deviceService.sendControl(uuid, command);
        return R.setOK("指令发送中");
    }
}
